using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using Geekees.App.Cashbook.Lib;
using Geekees.App.Cashbook.Lib.BLL;
using Geekees.App.Cashbook.Lib.Entity;
using Geekees.App.Cashbook.Lib.Helper;

namespace Geekees.App.Cashbook.Web
{
	/// <summary>
	/// Summary description for Category.
	/// </summary>
	public class Category : PageBase
	{
		protected System.Web.UI.WebControls.DropDownList ddlCategory;
		protected System.Web.UI.WebControls.Button btnSave;
		protected System.Web.UI.WebControls.RadioButtonList rblClassification;
		protected System.Web.UI.WebControls.Button btnDelete;
		protected System.Web.UI.WebControls.TextBox txtCategory;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			if( !IsPostBack )
				BindData();
		}

		private void BindData()
		{
			Binder.BindClassification( rblClassification, ListPromptType.None );
			BindCategory();
		}

		private void BindCategory()
		{
			int selectedClassificationId = Consts.NO_ID;
			if( this.rblClassification.SelectedIndex != -1 )
			{
				selectedClassificationId = int.Parse( this.rblClassification.SelectedValue );
				Binder.BindCategory( this.ddlCategory, selectedClassificationId, ListPromptType.Add );
			}
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);

			this.btnDelete.Attributes.Add("onclick", "return confirm('Sure?')");
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
			this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);
			this.Load += new System.EventHandler(this.Page_Load);
			this.rblClassification.SelectedIndexChanged +=new EventHandler(rblClassification_SelectedIndexChanged);
			this.ddlCategory.SelectedIndexChanged +=new EventHandler(ddlCategory_SelectedIndexChanged);
		}
		#endregion

		private void btnSave_Click(object sender, System.EventArgs e)
		{
			DoSave();
		}

		private void DoSave()
		{
			if( this.txtCategory.Text.Trim() == string.Empty )
			{
				FlashMessage.SetFlash( "Please give a name befor saving!", FlashMessage.FlashMessageType.Error );
				return;
			}

			int selectedClassificationId = Consts.NO_ID;
			if( this.rblClassification.SelectedIndex != -1 )
			{
				try
				{
					selectedClassificationId = int.Parse( this.rblClassification.SelectedValue );
				}
				catch{}
			}

			if( selectedClassificationId == Consts.NO_ID )
			{
				FlashMessage.SetFlash( "Please select a classification!", FlashMessage.FlashMessageType.Error );
				return;
			}

			if( this.ddlCategory.SelectedIndex <= 0 )
			{
				int id = BLCategory.AddCategory( this.txtCategory.Text.Trim() , selectedClassificationId );
				if( id > 0 )
					FlashMessage.SetFlash( "Add Successful!");
			}
			else
			{
				bool r = BLCategory.UpdateCategory( int.Parse( this.ddlCategory.SelectedValue ), this.txtCategory.Text.Trim(), selectedClassificationId  ) ;
				if( r )
					FlashMessage.SetFlash( "Update Successful!" );
				else
					FlashMessage.SetFlash( "Update Failed!", FlashMessage.FlashMessageType.Error );
			}	
			this.txtCategory.Text = string.Empty;
			BindCategory();
		}

		private void btnDelete_Click(object sender, System.EventArgs e)
		{
			if( this.ddlCategory.SelectedIndex <= 0 )
			{
				FlashMessage.SetFlash( "Please select one item before deleting!" , FlashMessage.FlashMessageType.Error);
				return;
			}

			bool r = BLCategory.DeleteCategory( int.Parse( this.ddlCategory.SelectedValue ) ) ;
			if( r )
				FlashMessage.SetFlash( "Delete Successful!" );
			else
				FlashMessage.SetFlash( "Delete Failed!", FlashMessage.FlashMessageType.Error );

			this.txtCategory.Text = string.Empty;
			this.BindCategory();
		}

		private void rblClassification_SelectedIndexChanged(object sender, EventArgs e)
		{
			BindCategory();
		}

		private void ddlCategory_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.txtCategory.Text = string.Empty;
			if( this.ddlCategory.SelectedIndex != 0 )
				this.txtCategory.Text = this.ddlCategory.SelectedItem.Text;
		}
	}
}
